define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'official/official_category/index' + location.search,
                    config_url: 'official/official_category/config',
                    add_url: 'official/official_category/add',
                    edit_url: 'official/official_category/edit',
                    del_url: 'official/official_category/del',
                    multi_url: 'official/official_category/multi',
                    import_url: 'official/official_category/import',
                    table: 'official_category',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left', formatter:function (value, row, index) {
                                var web_url = row.web_url;
                                if(web_url.length){
                                    return "<a target=\"_blank\" href='"+row.web_url+"' style=\"text-decoration:none;color: black;cursor:pointer\" title='" + row.subheading + "'>"+value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;')+"</a>";
                                }
                                return "<text style=\"cursor:pointer\" title='" + row.subheading + "'>"+value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;')+"</text>";
                            }
                        },
                        {field: 'e_name', title: __('E_name')},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'official_rule_name', title: __('Official_rule_id')},
                        {field: 'is_nav_show', title: __('Is_nav_show'), searchList: {"0":__('Is_nav_show 0'),"1":__('Is_nav_show 1')}, formatter: Table.api.formatter.normal},
                        {field: 'is_flooter_nav_show', title: __('Is_flooter_nav_show'), searchList: {"0":__('Is_flooter_nav_show 0'),"1":__('Is_flooter_nav_show 1')}, formatter: Table.api.formatter.normal},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('Querying Menu Rules'),
                                    title: __('Querying Menu Rules'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'official/official_category/getRule',
                                    // confirm: '确认发送',
                                    success: function (data, ret) {
                                        console.log()
                                        if (ret.code != 1){
                                            layer.msg(ret.msg, {icon: 5});
                                            return false;
                                        }
                                        //询问框
                                        layer.alert(ret.data, {
                                            skin: 'layui-layer-molv', //样式类名
                                            closeBtn: 0,
                                            title:'请复制规则！并使用在菜单规则中！'
                                        });

                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        // Layer.msg();
                                        //信息框-例4
                                        layer.msg(ret.msg, {icon: 5});
                                        return false;
                                    }
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-dragsort", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                // $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                // $(table).data("operate-edit", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $('.btn-config').click(function (){
                Fast.api.open('official/official_category/config', '设置首页默认栏目');
            });


        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'official/official_category/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'official/official_category/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'official/official_category/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        config: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});